<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="TCrbtProvinceOrderForm">
          <a-row>
						<a-col :span="12">
							<a-form-item label="手机号" v-bind="validateInfos.mobile" id="TCrbtProvinceOrderForm-mobile" name="mobile">
								<a-input v-model:value="formData.mobile" placeholder="请输入手机号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="业务名称" v-bind="validateInfos.cpparam" id="TCrbtProvinceOrderForm-cpparam" name="cpparam">
								<a-input v-model:value="formData.cpparam" placeholder="请输入业务名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="渠道号" v-bind="validateInfos.channel" id="TCrbtProvinceOrderForm-channel" name="channel">
								<a-input v-model:value="formData.channel" placeholder="请输入渠道号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="业务号" v-bind="validateInfos.cpid" id="TCrbtProvinceOrderForm-cpid" name="cpid">
								<a-input v-model:value="formData.cpid" placeholder="请输入业务号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="订单号" v-bind="validateInfos.oid" id="TCrbtProvinceOrderForm-oid" name="oid">
								<a-input v-model:value="formData.oid" placeholder="请输入订单号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="状态码" v-bind="validateInfos.rescode" id="TCrbtProvinceOrderForm-rescode" name="rescode">
								<a-input v-model:value="formData.rescode" placeholder="请输入状态码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="错误信息" v-bind="validateInfos.resmsg" id="TCrbtProvinceOrderForm-resmsg" name="resmsg">
								<a-input v-model:value="formData.resmsg" placeholder="请输入错误信息"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="省份" v-bind="validateInfos.province" id="TCrbtProvinceOrderForm-province" name="province">
								<a-input v-model:value="formData.province" placeholder="请输入省份"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="金额(分)" v-bind="validateInfos.fee" id="TCrbtProvinceOrderForm-fee" name="fee">
								<a-input v-model:value="formData.fee" placeholder="请输入金额(分)"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="美播业务" v-bind="validateInfos.keeptype" id="TCrbtProvinceOrderForm-keeptype" name="keeptype">
								<j-dict-select-tag v-model:value="formData.keeptype" dictCode="yn" placeholder="请选择美播业务"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="备注" v-bind="validateInfos.remark" id="TCrbtProvinceOrderForm-remark" name="remark">
								<a-textarea v-model:value="formData.remark" :rows="4" placeholder="请输入备注" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="创建时间" v-bind="validateInfos.createtime" id="TCrbtProvinceOrderForm-createtime" name="createtime">
								<a-date-picker placeholder="请选择创建时间"  v-model:value="formData.createtime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="修改时间" v-bind="validateInfos.updatetime" id="TCrbtProvinceOrderForm-updatetime" name="updatetime">
								<a-date-picker placeholder="请选择修改时间"  v-model:value="formData.updatetime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getDateByPicker, getValueType } from '/@/utils';
  import { saveOrUpdate } from '../TCrbtProvinceOrder.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    mobile: '',   
    cpparam: '',   
    channel: '',   
    cpid: '',   
    oid: '',   
    rescode: '',   
    resmsg: '',   
    province: '',   
    fee: '',   
    keeptype: '',   
    remark: '',   
    createtime: '',   
    updatetime: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    mobile: [{ required: true, message: '请输入手机号!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
  //日期个性化选择
  const fieldPickers = reactive({
  });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      // 更新个性化日期选择器的值
      model[data] = getDateByPicker(model[data], fieldPickers[data]);
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
